﻿using UnityEngine;
using System.Collections;

public class RoamingController : MonsterController
{
	// Properties

	[SerializeField]
	float				m_roamingBaseTime	= 2f;
	[SerializeField]
	float				m_roamingTimeRandom	= 1f;
	[SerializeField]
	float				m_roamingTurningDelay	= 0f;

	[SerializeField]
	bool				m_smartMode	= false;			// 지면인식 모드 사용할지 여부
	[SerializeField]
	bool				m_groundPatrol	= false;		// 언덕이나 낭떠러지를 오르내리지 않음

	protected override void OnInitialize ()
	{
		base.OnInitialize ();

		useGroundDetector	= m_smartMode;
		StartCoroutine(Roaming());
	}

	IEnumerator Roaming()
	{
		moving	= true;

		character.currentDirection	= Random.Range(0, 10) < 5? BaseCharacter.Direction.Left : BaseCharacter.Direction.Right;

		while(true)
		{
			yield return new WaitForSeconds
				(m_roamingBaseTime + Random.Range(0, m_roamingTimeRandom));

			moving	= false;
			yield return new WaitForSeconds(m_roamingTurningDelay);
			moving	= true;

			BaseCharacter.Direction dir	= character.currentDirection;
			switch(dir)
			{
			case BaseCharacter.Direction.Left:
				dir	= BaseCharacter.Direction.Right;
				break;
				
			default:
				dir	= BaseCharacter.Direction.Left;
				break;
			}
			character.currentDirection	= dir;
		}
	}

	protected override void OnUpdate()
	{
		base.OnUpdate();

		if(m_groundPatrol)
		{
			if(noGroundBelow || groundAhead)
				moving	= false;
		}
		else
		{
			if (groundAhead)				// 벽 앞에서는 점프
				character.Jump();

			if (dropAhead)					// 낭떠러지에서는 스탑
				moving	= false;
		}
	}
}
